<template>
  <el-form label-width="0px" class="wrap">
      <div class="title">解析存档的modoverrides.lua文件<br/>
        自动生成dedicated_server_mods_setup_rail.lua文件内容<br/>
        实现专服mod自动下载和更新。
      </div>
    <el-form-item label="">
      <el-input v-model="form.modoverrides" label="">
        <template #append>
          <el-button @click="getModoverrides">modoverrides.lua路径</el-button>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item label="" >
      <el-input v-model="form.rail" label="">
        <template #append>
          <el-button @click="getRail">dedicated_server_mods_setup_rail.lua路径</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-switch
      v-model="form.cover"
      active-text=""
      inactive-text="是否覆盖更新">
</el-switch>

    <el-form-item label="">
      <el-button type="primary" @click="serverUpdate">执行</el-button>
    </el-form-item>
    <el-scrollbar max-height="120px" :always="true">
      <div v-html="form.status" class="status"></div>
    </el-scrollbar>
  </el-form>

</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
const electronAPI = window.electronAPI
import { ElMessage, ElMessageBox } from 'element-plus'

const form = reactive({
  modoverrides: '',
  rail: '',
  status: '',
  cover: false,
})

onMounted(async () => {
  form.rail = await electronAPI.getConfig('dedicated_server_mods_setup_rail')  
  form.modoverrides = await electronAPI.getConfig('modoverrides')  
})

const getRail = async () => {
  let path = await electronAPI.selectFile({
    title: "dedicated_server_mods_setup_rail.lua路径",
    properties: ['openFile'],
    setkey: "dedicated_server_mods_setup_rail",
    filters: [
      { name: 'dedicated_server_mods_setup_rail.lua', extensions: ['lua'] },
    ]
  })  
  console.log(path)
  if (path) {
    form.rail = path[0]
  }
}

const getModoverrides = async () => {
  let path = await electronAPI.selectFile({
    title: "modoverrides.lua路径",
    properties: ['openFile'],
    setkey: "modoverrides",
    filters: [
      { name: 'modoverrides.lua', extensions: ['lua'] },
    ]
  })  
  console.log(path)
  if (path) {
    form.modoverrides = path[0]
  }

}

const serverUpdate = async () => {
  if (!form.modoverrides) {
    ElMessageBox.alert('请设置modoverrides.lua路径', '请确认', {
      confirmButtonText: '好的',
    })
    return
  }
  if (!form.rail) {
    ElMessageBox.alert('请设置dedicated_server_mods_setup_rail.lua路径', '请确认', {
      confirmButtonText: '好的',
    })
    return
  }

  let res = await electronAPI.serverUpdate(form.modoverrides, form.rail, form.cover)
  if (res.err) {
    ElMessageBox.alert('执行异常：'+res.err, '请确认', {
      confirmButtonText: '好的',
    })
    return
  }
  form.status = "执行完成，已用以下内容覆盖dedicated_server_mods_setup_rail.lua<br/>"+
  "-------------------------------<br/>" + 
  res.str.replaceAll("\n",  "<br/>")
}

electronAPI.onNameUpdate((value) => {
 form.status = "解析中-"+value
})

</script>

<style>
  .status{
    width: 100%;
  }
</style>

